<template>
  <div class="playlist-detail">
    <AppHeader />
    <main class="main-content">
      <div v-if="loading" class="loading">加载中...</div>
      <div v-else-if="error" class="error">{{ error }}</div>
      <template v-else>
        <!-- 歌单信息 -->
        <div class="playlist-info">
          <div class="cover">
            <img :src="playlist.picUrl" :alt="playlist.name">
          </div>
          <div class="info">
            <h1>{{ playlist.name }}</h1>
            <div class="creator" v-if="playlist.creator">
              <img :src="playlist.creator.avatarUrl" :alt="playlist.creator.name" class="avatar">
              <span class="name">{{ playlist.creator.name }}</span>
              <span class="create-time">创建时间：2024-01-17</span>
            </div>
            <div class="stats">
              <span>播放：{{ formatPlayCount(playlist.playCount) }}</span>
              <span>歌曲：{{ playlist.songCount }}</span>
            </div>
            <div class="desc" v-if="playlist.desc">
              <p>{{ playlist.desc }}</p>
            </div>
            <div class="actions">
              <button class="play-all" @click="playAll">
                <Icon name="play" />
                播放全部
              </button>
              <button class="collect">
                <Icon name="heart" />
                收藏
              </button>
            </div>
          </div>
        </div>

        <!-- 歌曲列表 -->
        <div class="song-list">
          <div class="list-header">
            <span class="index">#</span>
            <span class="title">歌曲</span>
            <span class="artist">歌手</span>
            <span class="album">专辑</span>
            <span class="duration">时长</span>
          </div>
          <SongItem
            v-for="(song, index) in playlist.list"
            :key="song.id"
            :song="song"
            :index="index + 1"
          />
        </div>
      </template>
    </main>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { usePlayerStore } from '@/stores/player'
import { playlistAPI } from '@/api'
import type { PlaylistInfo } from '@/types'
import { formatPlayCount } from '@/utils'
import AppHeader from '@/components/AppHeader.vue'
import SongItem from '@/components/SongItem.vue'
import Icon from '@/components/Icon.vue'

const route = useRoute()
const playerStore = usePlayerStore()
const loading = ref(true)
const error = ref('')
const playlist = ref<PlaylistInfo>({} as PlaylistInfo)

const playAll = () => {
  if (!playlist.value.list?.length) return
  playerStore.clearPlaylist()
  playlist.value.list.forEach(song => playerStore.addToPlaylist(song))
  playerStore.play(playlist.value.list[0])
}

onMounted(async () => {
  try {
    const id = route.params.id as string
    const res = await playlistAPI.getPlaylistDetail(id)
    playlist.value = res.data
  } catch (err) {
    console.error('Failed to fetch playlist:', err)
    error.value = '加载失败，请稍后重试'
  } finally {
    loading.value = false
  }
})
</script>

<style scoped>
.playlist-detail {
  min-height: 100vh;
  background: #f5f5f5;
}

.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.playlist-info {
  display: flex;
  gap: 30px;
  padding: 30px;
  background: #fff;
  border-radius: 8px;
  margin-bottom: 20px;
}

.cover {
  flex-shrink: 0;
  width: 200px;
  height: 200px;
}

.cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
}

.info {
  flex: 1;
  min-width: 0;
}

.info h1 {
  font-size: 24px;
  margin-bottom: 16px;
  color: #333;
}

.creator {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.creator .avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
}

.creator .name {
  color: var(--primary-color);
}

.creator .create-time {
  color: #999;
  font-size: 12px;
}

.stats {
  display: flex;
  gap: 20px;
  margin-bottom: 16px;
  color: #666;
}

.desc {
  color: #666;
  line-height: 1.6;
  margin-bottom: 20px;
}

.actions {
  display: flex;
  gap: 12px;
}

.actions button {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 24px;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
}

.play-all {
  background: var(--primary-color);
  color: #fff;
}

.collect {
  background: #f5f5f5;
  color: #666;
}

.song-list {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
}

.list-header {
  display: grid;
  grid-template-columns: 60px 1fr 200px 200px 100px;
  padding: 10px;
  border-bottom: 1px solid #eee;
  color: #999;
}

.loading,
.error {
  text-align: center;
  padding: 40px;
  background: #fff;
  border-radius: 8px;
}

.error {
  color: #ff4d4f;
}
</style> 